package com.yc.spring.bank;

import com.google.gson.Gson;
import com.yc.spring.bank.bean.Record;
import com.yc.spring.jdbc.RecordDao;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/queryRecord")
public class BankServlet extends javax.servlet.http.HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        WebApplicationContext webApplicationContext =
                WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

        resp.setContentType("text/html;charset=utf-8");
        RecordDao recordDao = (RecordDao) webApplicationContext.getBean("recordDao");
        final List<Record> records = recordDao.selectByAid(123456);
        Gson gson = new Gson();
        final String json = gson.toJson(records);
        resp.getWriter().println(json);
    }
}
